package com.ja.openchat.ui.chat;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.ja.openchat.BR;
import com.ja.openchat.R;
import com.ja.openchat.ui.base.JaBaseActivity;
import com.zy.openchat.core.manager.ChatExCacheManager;

import org.jetbrains.annotations.NotNull;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class ChooseBackgoundActivity extends JaBaseActivity {

    public static final String RESULT = "result";

    @BindView(R.id.tv_ok_right)
    TextView tv_ok_right;
    private RecyclerView recyclerView;

    @Override
    public void initData() {
        setTitle(getString(R.string.choose_backgroun_image));
        tv_ok_right.setVisibility(View.VISIBLE);
        tv_ok_right.setText(getString(R.string.save_text));
        recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new GridLayoutManager(this,4));
        recyclerView.setAdapter(backgoundAdapter);
        backgoundAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter, @NonNull @NotNull View view, int position) {
//                Intent intent = new Intent();
//                intent.putExtra(RESULT,backgoundAdapter.getItem(position));
//                setResult(RESULT_OK,intent);
//                finish();
                PreviewLocalImageActivity.previewMedia(ChooseBackgoundActivity.this,backgoundAdapter.getData(),position);
            }
        });
        List<String> bgrounds = ChatExCacheManager.defaultChatBgList();
        bgrounds.add(0,"shape_gray_background");
        backgoundAdapter.setNewInstance(bgrounds);
        doButtonClick();
    }

    public void setTitle(String title) {
        TextView titleText = findViewById(R.id.tv_bar_title);
        if (titleText != null) {
            titleText.setText(title);
        }
    }
    protected boolean isHaveBackButton() {
        return true;
    }

    private void doButtonClick() {
        View backView = findViewById(R.id.iv_bar_back);
        if (backView != null) {
            if (isHaveBackButton()) {
                backView.setVisibility(View.VISIBLE);
                backView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        finish();
                    }
                });
            } else {
                backView.setVisibility(View.GONE);
            }
        }
    }
    private int currentChoosePosition = -1;

    private BaseQuickAdapter<String, BaseViewHolder> backgoundAdapter = new BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_choose_bg) {
        @Override
        protected void convert(@NotNull BaseViewHolder helper, String s) {
            ImageView imageView = helper.getView(R.id.imageView);
            if(helper.getAdapterPosition()==0){
                imageView.setImageResource(getResourceByDrawable(s));
            }else{
                imageView.setImageResource(getResource(s));
            }
            CheckBox checkBox = helper.getView(R.id.checkbox);
            checkBox.setOnCheckedChangeListener(null);
            checkBox.setChecked(currentChoosePosition == helper.getAdapterPosition());
            checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if(isChecked){
                        int temp = currentChoosePosition;
                        currentChoosePosition = helper.getAdapterPosition();
                        if(currentChoosePosition!=temp){
                            notifyItemChanged(temp);
                        }
                    }else{
                        currentChoosePosition = -1;
                    }
                }
            });

        }
    };

    @OnClick(R.id.tv_ok_right)
    public void onClick(){
        if(currentChoosePosition==-1){
            ToastUtils.showShort(getString(R.string.tick_background));
            return;
        }
        Intent intent = new Intent();
        if(currentChoosePosition == 0){
            intent.putExtra(RESULT,"");
        }else{
            intent.putExtra(RESULT,backgoundAdapter.getItem(currentChoosePosition));
        }
        setResult(RESULT_OK,intent);
        finish();
    }

    public int  getResource(String imageName){
        return getResources().getIdentifier(imageName, "mipmap", getPackageName());
    }

    public int  getResourceByDrawable(String imageName){
        return getResources().getIdentifier(imageName, "drawable", getPackageName());
    }

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_choose_background;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    protected boolean isHaveButterKnife() {
        return true;
    }
}
